package service.temp.job;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;

public class SplitItemList {

    public static void main(String[] args) {
        List<String> lineList = loadLineList("/Users/zhfan/Downloads/target_01.txt");

        Set<String> allItemSet = new HashSet<>();
        for (String line : lineList) {
            String targetItemId = line.split(", source: \\[")[0].split("requestItem: ")[1];
            allItemSet.add(targetItemId);

            String[] split = line.split("ScanItemDto\\(");
            for (String sub : split) {
                if (sub.contains("itemId=") && sub.contains(", jobHistoryId")) {
                    String curSourceItemId = sub.split("itemId=")[1].split(", jobHistoryId")[0];
                    allItemSet.add(curSourceItemId);
                }
            }
        }

//        System.out.println(allItemSet.toString());
//        System.out.println(allItemSet.size());

//        Set<String> excelSet = new HashSet<>(loadLineList("/Users/zhfan/Downloads/batch_excel_id_list.txt"));
//        for (String id : excelSet) {
//            if (!allItemSet.contains(id)) {
//                System.out.println(" --- " + id);
//            }
//        }

        List<String> mergeLineList = loadLineList("/Users/zhfan/Downloads/merge_result.txt");

        Set<String> mergeItemSet = new HashSet<>();
        for (String line : mergeLineList) {
            String targetItemId = line.split("targetItemId: ")[1].split(", sourceId: ")[0];
            mergeItemSet.add(targetItemId);

            String[] split = line.split(", sourceId: \\[")[1].split("]")[0].split(",");
            for (String sub : split) {
                if (sub.length() > 4) {
                    mergeItemSet.add(sub);
                }
            }
        }

        Set<String> excelSet = new HashSet<>(loadLineList("/Users/zhfan/Downloads/batch_excel_id_list.txt"));
        for (String id : excelSet) {
            if (!mergeItemSet.contains(id)) {
                System.out.println(" --- " + id);
            }
        }

    }

    public static List<String> loadLineList(String fileName) {
        BufferedReader br = null;
        String line = "";
        List<String> candidateList = new ArrayList<>();

        try {
            br = new BufferedReader(new FileReader(fileName));
            while ((line = br.readLine()) != null) {
                if (StringUtils.isBlank(line)) continue;
                candidateList.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return candidateList;
    }
}
